/*
*********************************************************************************************************
* uC/OS-II port for Keil C
* By John X. Liu, China, (johnxliu@163.com)
*
* IMPORTANT:
* Due to the special grammer of Keil C, all functions by default are non-reentrant. But uC/OS-II 
* functions must be reentrant, so it is needed to modify the declarations of all functions in head files 
* and c source files.
* Now this file tells what the functions should be. Thus if you get any 'redefinition error' at compiling 
* time, don't modify this file but to modify the head files and c source files to make them conform to 
* the declarations in this file.
*********************************************************************************************************
*/
#ifndef __OS_KC51_H
#define __OS_KC51_H

/*
*********************************************************************************************************
*                                         EVENT FLAGS MANAGEMENT
*********************************************************************************************************
*/

#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)

#if OS_FLAG_ACCEPT_EN > 0
OS_FLAGS      OSFlagAccept(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT8U *err) KCREENTRANT;
#endif

OS_FLAG_GRP  *OSFlagCreate(OS_FLAGS flags, INT8U *err) KCREENTRANT;

#if OS_FLAG_DEL_EN > 0
OS_FLAG_GRP  *OSFlagDel(OS_FLAG_GRP *pgrp, INT8U opt, INT8U *err) KCREENTRANT;
#endif

OS_FLAGS      OSFlagPend(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *err) KCREENTRANT;
OS_FLAGS      OSFlagPost(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U operation, INT8U *err) KCREENTRANT;

#if OS_FLAG_QUERY_EN > 0
OS_FLAGS      OSFlagQuery(OS_FLAG_GRP *pgrp, INT8U *err) KCREENTRANT;
#endif
#endif

/*
*********************************************************************************************************
*                                        MESSAGE MAILBOX MANAGEMENT
*********************************************************************************************************
*/

#if OS_MBOX_EN > 0

#if OS_MBOX_ACCEPT_EN > 0
void         *OSMboxAccept(OS_EVENT *pevent) KCREENTRANT;
#endif

OS_EVENT     *OSMboxCreate(void *msg) KCREENTRANT;

#if OS_MBOX_DEL_EN > 0
OS_EVENT     *OSMboxDel(OS_EVENT *pevent, INT8U opt, INT8U *err) KCREENTRANT;
#endif

void         *OSMboxPend(OS_EVENT *pevent, INT16U timeout, INT8U *err) KCREENTRANT;

#if OS_MBOX_POST_EN > 0
INT8U         OSMboxPost(OS_EVENT *pevent, void *msg) KCREENTRANT;
#endif

#if OS_MBOX_POST_OPT_EN > 0
INT8U         OSMboxPostOpt(OS_EVENT *pevent, void *msg, INT8U opt) KCREENTRANT;
#endif

#if OS_MBOX_QUERY_EN > 0
INT8U         OSMboxQuery(OS_EVENT *pevent, OS_MBOX_DATA *pdata) KCREENTRANT;
#endif
#endif

/*
*********************************************************************************************************
*                                           MEMORY MANAGEMENT
*********************************************************************************************************
*/

#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)

OS_MEM       *OSMemCreate(void *addr, INT32U nblks, INT32U blksize, INT8U *err) KCREENTRANT;
void         *OSMemGet(OS_MEM *pmem, INT8U *err) KCREENTRANT;
INT8U         OSMemPut(OS_MEM *pmem, void *pblk) KCREENTRANT;

#if OS_MEM_QUERY_EN > 0
INT8U         OSMemQuery(OS_MEM *pmem, OS_MEM_DATA *pdata) KCREENTRANT;
#endif

#endif

/*
*********************************************************************************************************
*                                MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
*********************************************************************************************************
*/

#if OS_MUTEX_EN > 0

#if OS_MUTEX_ACCEPT_EN > 0
INT8U         OSMutexAccept(OS_EVENT *pevent, INT8U *err) KCREENTRANT;
#endif

OS_EVENT     *OSMutexCreate(INT8U prio, INT8U *err) KCREENTRANT;

#if OS_MUTEX_DEL_EN > 0
OS_EVENT     *OSMutexDel(OS_EVENT *pevent, INT8U opt, INT8U *err) KCREENTRANT;
#endif

void          OSMutexPend(OS_EVENT *pevent, INT16U timeout, INT8U *err) KCREENTRANT;
INT8U         OSMutexPost(OS_EVENT *pevent) KCREENTRANT;

#if OS_MUTEX_QUERY_EN > 0
INT8U         OSMutexQuery(OS_EVENT *pevent, OS_MUTEX_DATA *pdata) KCREENTRANT;
#endif

#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                         MESSAGE QUEUE MANAGEMENT
*********************************************************************************************************
*/

#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)

#if OS_Q_ACCEPT_EN > 0
void         *OSQAccept(OS_EVENT *pevent) KCREENTRANT;
#endif

OS_EVENT     *OSQCreate(void **start, INT16U size) KCREENTRANT;

#if OS_Q_DEL_EN > 0
OS_EVENT     *OSQDel(OS_EVENT *pevent, INT8U opt, INT8U *err) KCREENTRANT;
#endif

#if OS_Q_FLUSH_EN > 0
INT8U         OSQFlush(OS_EVENT *pevent) KCREENTRANT;
#endif

void         *OSQPend(OS_EVENT *pevent, INT16U timeout, INT8U *err) KCREENTRANT;

#if OS_Q_POST_EN > 0
INT8U         OSQPost(OS_EVENT *pevent, void *msg) KCREENTRANT;
#endif

#if OS_Q_POST_FRONT_EN > 0
INT8U         OSQPostFront(OS_EVENT *pevent, void *msg) KCREENTRANT;
#endif

#if OS_Q_POST_OPT_EN > 0
INT8U         OSQPostOpt(OS_EVENT *pevent, void *msg, INT8U opt) KCREENTRANT;
#endif

#if OS_Q_QUERY_EN > 0
INT8U         OSQQuery(OS_EVENT *pevent, OS_Q_DATA *pdata) KCREENTRANT;
#endif

#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                          SEMAPHORE MANAGEMENT
*********************************************************************************************************
*/
#if OS_SEM_EN > 0

#if OS_SEM_ACCEPT_EN > 0
INT16U        OSSemAccept(OS_EVENT *pevent) KCREENTRANT;
#endif

OS_EVENT     *OSSemCreate(INT16U cnt) KCREENTRANT;

#if OS_SEM_DEL_EN > 0
OS_EVENT     *OSSemDel(OS_EVENT *pevent, INT8U opt, INT8U *err) KCREENTRANT;
#endif

void          OSSemPend(OS_EVENT *pevent, INT16U timeout, INT8U *err) KCREENTRANT;
INT8U         OSSemPost(OS_EVENT *pevent) KCREENTRANT;

#if OS_SEM_QUERY_EN > 0
INT8U         OSSemQuery(OS_EVENT *pevent, OS_SEM_DATA *pdata) KCREENTRANT;
#endif

#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                            TASK MANAGEMENT
*********************************************************************************************************
*/
#if OS_TASK_CHANGE_PRIO_EN > 0
INT8U         OSTaskChangePrio(INT8U oldprio, INT8U newprio) KCREENTRANT;
#endif

#if OS_TASK_CREATE_EN > 0
INT8U         OSTaskCreate(void (*task)(void *pd) KCREENTRANT, void *pdata, OS_STK *ptos, INT8U prio) KCREENTRANT;
#endif

#if OS_TASK_CREATE_EXT_EN > 0
INT8U         OSTaskCreateExt(void  (*task)(void *pd) KCREENTRANT,
                              void   *pdata,
                              OS_STK *ptos,
                              INT8U   prio,
                              INT16U  id,
                              OS_STK *pbos,
                              INT32U  stk_size,
                              void   *pext,
                              INT16U  opt) KCREENTRANT;
#endif

#if OS_TASK_DEL_EN > 0
INT8U         OSTaskDel(INT8U prio) KCREENTRANT;
INT8U         OSTaskDelReq(INT8U prio) KCREENTRANT;
#endif

#if OS_TASK_SUSPEND_EN > 0
INT8U         OSTaskResume(INT8U prio) KCREENTRANT;
INT8U         OSTaskSuspend(INT8U prio) KCREENTRANT;
#endif

#if OS_TASK_CREATE_EXT_EN > 0
INT8U         OSTaskStkChk(INT8U prio, OS_STK_DATA *pdata) KCREENTRANT;
#endif

#if OS_TASK_QUERY_EN > 0
INT8U         OSTaskQuery(INT8U prio, OS_TCB *pdata) KCREENTRANT;
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                            TIME MANAGEMENT
*********************************************************************************************************
*/

void          OSTimeDly(INT16U ticks) KCREENTRANT;

#if OS_TIME_DLY_HMSM_EN > 0
INT8U         OSTimeDlyHMSM(INT8U hours, INT8U minutes, INT8U seconds, INT16U milli) KCREENTRANT;
#endif

#if OS_TIME_DLY_RESUME_EN > 0
INT8U         OSTimeDlyResume(INT8U prio) KCREENTRANT;
#endif

#if OS_TIME_GET_SET_EN > 0
INT32U        OSTimeGet(void) KCREENTRANT;
void          OSTimeSet(INT32U ticks) KCREENTRANT;
#endif

void          OSTimeTick(void) KCREENTRANT;

/*
*********************************************************************************************************
*                                             MISCELLANEOUS
*********************************************************************************************************
*/

void          OSInit(void) KCREENTRANT;

void          OSIntEnter(void) KCREENTRANT;
void          OSIntExit(void) KCREENTRANT;

#if OS_SCHED_LOCK_EN > 0
void          OS_SchedLock(void) KCREENTRANT;
void          OSSchedUnlock(void) KCREENTRANT;
#endif

void          OSStart(void) KCREENTRANT;

void          OSStatInit(void) KCREENTRANT;

INT16U        OSVersion(void) KCREENTRANT;

/*$PAGE*/
/*
*********************************************************************************************************
*                                      INTERNAL FUNCTION PROTOTYPES
*                            (Your application MUST NOT call these functions)
*********************************************************************************************************
*/

#if OS_TASK_DEL_EN > 0
void          OS_Dummy(void) KCREENTRANT;
#endif

#if ((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0) || (OS_SEM_EN > 0) || (OS_MUTEX_EN > 0)
INT8U         OS_EventTaskRdy(OS_EVENT *pevent, void *msg, INT8U msk) KCREENTRANT;
void          OS_EventTaskWait(OS_EVENT *pevent) KCREENTRANT;
void          OS_EventTO(OS_EVENT *pevent) KCREENTRANT;
void          OS_EventWaitListInit(OS_EVENT *pevent) KCREENTRANT;
#endif

#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
void          OS_FlagInit(void) KCREENTRANT;
void          OS_FlagUnlink(OS_FLAG_NODE *pnode) KCREENTRANT;
#endif

#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
void          OS_MemInit(void) KCREENTRANT;
#endif

#if OS_Q_EN > 0
void          OS_QInit(void) KCREENTRANT;
#endif

void          OS_Sched(void) KCREENTRANT;

void          OS_TaskIdle(void *data) KCREENTRANT;

#if OS_TASK_STAT_EN > 0
void          OS_TaskStat(void *data) KCREENTRANT;
#endif

INT8U         OS_TCBInit(INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U opt) KCREENTRANT;

/*$PAGE*/
/*
*********************************************************************************************************
*                                          FUNCTION PROTOTYPES
*                                      (Target Specific Functions)
*********************************************************************************************************
*/

#if OS_VERSION >= 204
void          OSInitHookBegin(void) KCREENTRANT;
void          OSInitHookEnd(void) KCREENTRANT;
#endif

void          OSIntCtxSw(void) KCREENTRANT;

void          OSStartHighRdy(void) KCREENTRANT;

void          OSTaskCreateHook(OS_TCB *ptcb) KCREENTRANT;
void          OSTaskDelHook(OS_TCB *ptcb) KCREENTRANT;

#if OS_VERSION >= 251
void          OSTaskIdleHook(void) KCREENTRANT;
#endif

void          OSTaskStatHook(void) KCREENTRANT;
OS_STK       *OSTaskStkInit(void (*task)(void *pd) KCREENTRANT, void *pdata, OS_STK *ptos, INT16U opt) KCREENTRANT;
void          OSTaskSwHook(void) KCREENTRANT;

#if OS_VERSION >= 204
void          OSTCBInitHook(OS_TCB *ptcb) KCREENTRANT;
#endif

void          OSTimeTickHook(void) KCREENTRANT;

#endif
